package com.winit.schedule.job.tracker.repository.mongo;

import java.util.List;

import com.winit.schedule.core.repository.entity.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.winit.schedule.core.model.AssignedTask;
import com.winit.schedule.core.model.JobID;
import com.winit.schedule.core.repository.LoggerRepository;
import com.winit.schedule.core.repository.constants.LogType;
import com.winit.schedule.core.utils.Clock;
import com.winit.schedule.job.tracker.repository.LogDataManager;

public class MongoLogDataManager implements LogDataManager {

    protected LoggerRepository            loggerRepository;

    private static transient final Logger logger = LoggerFactory.getLogger(MongoLogDataManager.class);

    public MongoLogDataManager(){
    }

    @Override
    public void logger(JobEntity job, List<String> taskIds, boolean updateJob) {
        try {
            LogEntity log = new LogEntity();

            log.setJobId(job.getJobId().getJobId());
            log.setLogTime(Clock.getSystemTime());
            if (updateJob) {
                log.setLogType(LogType.UPDATE_JOB);
            } else {
                log.setLogType(LogType.CREATE_JOB);
            }

            log.setUserAction(true);
            log.setContent("Job:" + JSON.toJSONString(job) + ", Task:" + JSON.toJSONString(taskIds));
            loggerRepository.saveLogger(log);
        } catch (Exception e) {
            logger.error("Logger save failure." + e);
        }
    }

    @Override
    public void logger(JobID jobId, LogType logType) {
        try {
            LogEntity log = new LogEntity();
            log.setJobId(jobId.getJobId());
            log.setLogTime(Clock.getSystemTime());
            log.setLogType(logType);
            log.setUserAction(true);
            log.setContent("JobId:" + JSON.toJSONString(jobId));
            loggerRepository.saveLogger(log);
        } catch (Exception e) {
            logger.error("Logger save failure." + e);
        }

    }

    @Override
    public void logger(List<AssignedTask> tasks, String exception, String taskTrackerUuid, LogType logType) {
        try {
            if (null != tasks) {
                LogEntity log = null;
                long currentTime = Clock.getSystemTime();
                for (AssignedTask task : tasks) {
                    log = new LogEntity();
                    log.setLogTime(currentTime);
                    log.setJobId(task.getTaskId().getJobId().getJobId());
                    log.setTaskId(task.getTaskId().getTaskId());
                    log.setLogType(logType);
                    log.setUserAction(false);
                    log.setTaskTrackerUuid(taskTrackerUuid);
                    log.setContent(exception);
                    loggerRepository.saveLogger(log);
                }
            }
        } catch (Exception e) {
            logger.error("Logger save failure." + e);
        }

    }

    @Override
    public void logger(List<AssignedTask> tasks, String taskTrackerUuid, LogType logType) {
        try {
            if (null != tasks) {
                LogEntity log = null;
                long currentTime = Clock.getSystemTime();
                for (AssignedTask task : tasks) {
                    log = new LogEntity();
                    log.setLogTime(currentTime);
                    log.setJobId(task.getTaskId().getJobId().getJobId());
                    log.setTaskId(task.getTaskId().getTaskId());
                    log.setLogType(logType);
                    log.setUserAction(false);
                    log.setTaskTrackerUuid(taskTrackerUuid);
                    log.setContent(JSON.toJSONString(tasks));
                    loggerRepository.saveLogger(log);
                }
            }
        } catch (Exception e) {
            logger.error("Logger save failure." + e);
        }

    }

    @Override
    public void logger(String taskTrackerUuid, LogType logType) {
        try {
            LogEntity log = new LogEntity();
            log.setLogTime(Clock.getSystemTime());
            log.setLogType(logType);
            log.setUserAction(false);
            log.setTaskTrackerUuid(taskTrackerUuid);
            loggerRepository.saveLogger(log);
        } catch (Exception e) {
            logger.error("Logger save failure." + e);
        }
    }

    @Override
    public void logger(QuartzLogEntity log) {
        // TODO Auto-generated method stub
        
    }

    @Override public void logger(TaskEntity task, LogType logType) {

    }

    @Override public void logger(String[] ids, LogType logType) {

    }

    @Override public void logger(UserEntity user, LogType logType) {

    }

}
